import cv2
import numpy as np

def subtract_images(image_path_a, image_path_b, save_path):
    # Read images as grayscale
    image_a = cv2.imread(image_path_a, cv2.IMREAD_GRAYSCALE)
    image_b = cv2.imread(image_path_b, cv2.IMREAD_GRAYSCALE)

    # Check if images are loaded correctly
    if image_a is None or image_b is None:
        print(f"Error reading images: {image_path_a} or {image_path_b}")
        return

    # Subtract image B from image A
    difference = image_a - image_b

    # Convert difference to unsigned 8-bit for saving
    difference = np.clip(difference, 0, 255).astype(np.uint8)

    # Save the difference image
    cv2.imwrite(save_path, difference)

if __name__ == "__main__":
    image_path_a = "predictions/images/69.png"  # Replace with the actual path to image A
    image_path_b = "predictions/images/70.png"  # Replace with the actual path to image B
    save_path = "difference.png"  # Replace with the desired path to save the difference image

    subtract_images(image_path_a, image_path_b, save_path)
    print(f"Difference image saved to: {save_path}")